package com.fishbone.sample.basic.resttemplate;

import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.util.UriComponentsBuilder;

import java.net.URI;
import java.util.Map;

/**
 * @ClassName RestTempleSample
 * @Author huyaxi
 * @DATE 2020/4/17 10:03
 */
public class RestTempleSample {

    public URI builderUri() {
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost");
        MultiValueMap<String,String> params = new LinkedMultiValueMap<>();
        params.add("userId",String.valueOf(1L));
        return builder.queryParams(params).build().encode().toUri();
    }


    /**
     * 构建Get请求的URI
     * @param baseUrl
     * @param params
     * @return
     */
    public URI builder(String baseUrl, Map<String,Object> params) {
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(baseUrl);
        MultiValueMap<String,String> multiValueMap = new LinkedMultiValueMap<>();
        params.forEach((key,value)->multiValueMap.add(key,String.valueOf(value)));
        return builder.queryParams(multiValueMap).build().encode().toUri();
    }
}
